package com.concurrency.juc.synctools.atomic;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicDemo1 {
    public static void main(String[] args) {
        AtomicDemo demo  = new AtomicDemo();
        for(int i=0;i<10;i++) {
            new Thread(demo).start();
        }
    }


    static class AtomicDemo implements Runnable{
        //改方法可能会获取重复的serialNumber
//        private int serialNumber = 0;
        private AtomicInteger serialNumber = new AtomicInteger();


        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + getSerialNumber());
        }

        public int getSerialNumber() {
            return serialNumber.getAndIncrement();
        }
    }
}
